/*
    监听命令行输入事件
*/



#include <event.h>
#include <unistd.h>
#include <stdlib.h>
#include <err.h>
#include <event2/event.h>
#include <iostream>

//键盘事件
void cmd_msg_cb(int FILENO, short type, void* _ev_base)
{
    printf("cmd_msg_cb\n");
    char buffer[1024];
    int ret = read(STDIN_FILENO, buffer, sizeof(buffer));
    if (ret < 0)
    {
        perror("read fail ");
        exit(1);
    }
    std::cout<< buffer;

    event_base *ev_base = (event_base*) _ev_base; // 传入全局的event base
    return; 
}

int main(int argc, char** argv)
{
    event_base *ev_base = event_init();
    //监听终端输入事件
    struct event *ev_cmd =(event*) calloc(1, sizeof(event));
    event_set(ev_cmd, STDIN_FILENO, EV_READ | EV_PERSIST, cmd_msg_cb, ev_base);
    event_add(ev_cmd, NULL);
    event_dispatch();
}



